package cn.edu.hznu.web.crawl;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class DownloadContent {
	
//	BasicCookieStore cookieStore = new BasicCookieStore();
//	BasicClientCookie
//	BasicHttpContext
//	HttpClientContext
	
	
	private static HttpEntity processResponse(HttpResponse response)
			throws ClientProtocolException {
		int status = response.getStatusLine().getStatusCode();
		if (status >= 200 && status < 300) {
			return response.getEntity();
		} else {
			throw new ClientProtocolException(
					"Unexpected response status: " + status);
		}
	}
	
	public static String getStrContentFormUrlWithCookie(
			String url, CookieStore cookieStore, String chatset) 
					throws ClientProtocolException, ParseException, IOException{
		
		try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
			HttpContext httpContext = new BasicHttpContext();
			httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
			return httpClient.execute(new HttpGet(url), (response) -> {
				HttpEntity entity = processResponse(response);
				return entity != null ? EntityUtils.toString(entity, chatset) : null;
			}, httpContext);
		}
	}
	
	public static String getStrContentFormUrlWithCookie(
			String url, String cookie, String chatset) 
					throws ClientProtocolException, ParseException, IOException{
		
		try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
			HttpGet httpGet = new HttpGet(url);
			httpGet.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Macintosh; "
					+ "Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0");
			httpGet.setHeader("Cookie", cookie);
			return httpClient.execute(httpGet, (response) -> {
				HttpEntity entity = processResponse(response);
				return entity != null ? EntityUtils.toString(entity, chatset) : null;
			} );
		}
	}
	
	
	
	public static String getStrContentFromUrl(String url, String charset) 
			throws ClientProtocolException, ParseException, IOException {
		
		try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
			return httpClient.execute(new HttpGet(url), (response) -> {
						HttpEntity entity = processResponse(response);
						return entity != null ?
								 EntityUtils.toString(entity, charset) : null;
					});
		}
	}
	
	public static byte[] getByteContentFromUrl(String url) 
			throws ClientProtocolException, IOException {
		
		try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
			return httpClient.execute(new HttpGet(url), (response) -> {
						HttpEntity entity = processResponse(response);
						return entity != null ?
								 EntityUtils.toByteArray(entity) : null;
				   });
		} 
	}
	
	
	public static void downloadImgFromUrl(String url, OutputStream outstream)
			throws ClientProtocolException, IOException {
		
		try (CloseableHttpResponse response = HttpClientBuilder
					.create()
					.build()
					.execute(new HttpGet(url))) {
			response.getEntity().writeTo(outstream);
		}
	}
	
	public static void downloadImgFromUrl(HttpClient httpClient, String url, OutputStream outstream) 
			throws IOException {
		
		httpClient.execute(new HttpGet(url)).getEntity().writeTo(outstream);
	}
}
